import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import test.string.StringCharCode;

public class DecodeBase64 {
	private static final int[] base64DecodeChars = { -1, -1, -1, -1, -1, -1,
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			-1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60,
			61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
			11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1,
			-1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
			39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1,
			-1 };

	static byte[] getBytes(ArrayList bytelist) {
		byte[] code = new byte[bytelist.size()];
		for (int i = 0; i < bytelist.size(); ++i) {
			code[i] = ((Byte) bytelist.get(i));
			// = (byte) c;
		}
		return code;
	}

	static byte[] base64decode(String str) throws UnsupportedEncodingException {
		byte c1 = 0, c2, c3, c4;
		int i = 0, len;
		ArrayList byteList = new ArrayList();

		byte[] by = str.getBytes();
		byte[] btemp = new byte[1];
		len = str.length();
		while (i < len) {
			/* c1 */
			do {
				c1 = (byte) base64DecodeChars[by[i++] & 0xff];
			} while (i < len && c1 == -1);
			if (c1 == -1) {
				break;
			}
			/* c2 */
			do {
				c2 = (byte) base64DecodeChars[by[i++] & 0xff];
			} while (i < len && c2 == -1);
			if (c2 == -1)
				break;
			btemp[0] = (byte) ((c1 << 2) | ((c2 & 0x30) >> 4));
			// out += new String(btemp,"UTF-8");
			byteList.add(btemp[0]);
			/* c3 */
			do {
				c3 = (byte) (by[i++] & 0xff);
				if (c3 == 61)
					return getBytes(byteList);
				c3 = (byte) base64DecodeChars[c3];
			} while (i < len && c3 == -1);
			if (c3 == -1)
				break;

			btemp[0] = (byte) (((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
			byteList.add(btemp[0]);

			/* c4 */
			do {
				c4 = (byte) (by[i++] & 0xff);
				if (c4 == 61)
					return getBytes(byteList);
				c4 = (byte) base64DecodeChars[c4];
			} while (i < len && c4 == -1);
			if (c4 == -1)
				break;
			btemp[0] = (byte) (((c3 & 0x03) << 6) | c4);
			byteList.add(btemp[0]);
		}
		return getBytes(byteList);
	}

	/**
	 * @param args
	 * @throws UnsupportedEncodingException
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		String testCode = "ZGpm5Lqs5Lic5pa577+lIyPvv6UjJeKApuKApiPvvJrigJzigJ3jgIvvvJ/jgIvvvKDigKbigKbvvIbDl8OX77yg77yD77yg77yB77yM772L77yS77yT77yU5ZKv";

		byte[] utf8Code = base64decode(testCode);
		System.out.println(StringCharCode.getHexString(utf8Code));

		String str = new String(utf8Code, "UTF-8");
		System.out.println(StringCharCode.getHexString(str.getBytes("UTF-8")));
		System.out.println(str);

		String str2 = new String(str.getBytes("UTF-8"), "GBK");
		System.out.println(str2);
	}

}
